#!/bin/bash

# 云服务器状态和网络连接诊断脚本
# 用于检查服务器 47.237.10.129 的详细状态

SERVER_IP="47.237.10.129"
LOG_FILE="server-diagnostic-$(date +%Y%m%d-%H%M%S).log"

echo "=== 云服务器诊断报告 ===" | tee $LOG_FILE
echo "服务器IP: $SERVER_IP" | tee -a $LOG_FILE
echo "检查时间: $(date)" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE

# 1. 基础网络连通性测试
echo "1. 网络连通性测试" | tee -a $LOG_FILE
echo "-------------------" | tee -a $LOG_FILE

# Ping测试
echo "[Ping测试]" | tee -a $LOG_FILE
if ping -c 4 $SERVER_IP >/dev/null 2>&1; then
    echo "✓ Ping成功 - 服务器网络可达" | tee -a $LOG_FILE
    ping -c 4 $SERVER_IP | tee -a $LOG_FILE
else
    echo "✗ Ping失败 - 服务器可能宕机或网络不通" | tee -a $LOG_FILE
fi
echo "" | tee -a $LOG_FILE

# 2. 端口连接测试
echo "2. 端口连接测试" | tee -a $LOG_FILE
echo "-------------------" | tee -a $LOG_FILE

PORTS=("22" "80" "443" "3000" "8080")
for port in "${PORTS[@]}"; do
    echo "[端口 $port 测试]" | tee -a $LOG_FILE
    if timeout 10 bash -c "</dev/tcp/$SERVER_IP/$port" 2>/dev/null; then
        echo "✓ 端口 $port 开放" | tee -a $LOG_FILE
    else
        echo "✗ 端口 $port 关闭或被阻止" | tee -a $LOG_FILE
    fi
done
echo "" | tee -a $LOG_FILE

# 3. HTTP/HTTPS服务测试
echo "3. Web服务测试" | tee -a $LOG_FILE
echo "-------------------" | tee -a $LOG_FILE

# HTTP测试
echo "[HTTP服务测试]" | tee -a $LOG_FILE
for port in 80 8080 3000; do
    echo "测试 http://$SERVER_IP:$port" | tee -a $LOG_FILE
    if curl -m 10 -s -o /dev/null -w "HTTP状态码: %{http_code}, 响应时间: %{time_total}s\n" "http://$SERVER_IP:$port" 2>/dev/null | tee -a $LOG_FILE; then
        echo "✓ HTTP服务在端口$port响应" | tee -a $LOG_FILE
    else
        echo "✗ HTTP服务在端口$port无响应" | tee -a $LOG_FILE
    fi
done

# HTTPS测试
echo "[HTTPS服务测试]" | tee -a $LOG_FILE
echo "测试 https://$SERVER_IP" | tee -a $LOG_FILE
if curl -m 10 -s -o /dev/null -w "HTTP状态码: %{http_code}, 响应时间: %{time_total}s\n" "https://$SERVER_IP" 2>/dev/null | tee -a $LOG_FILE; then
    echo "✓ HTTPS服务响应" | tee -a $LOG_FILE
else
    echo "✗ HTTPS服务无响应" | tee -a $LOG_FILE
fi
echo "" | tee -a $LOG_FILE

# 4. DNS解析测试
echo "4. DNS解析测试" | tee -a $LOG_FILE
echo "-------------------" | tee -a $LOG_FILE
echo "[正向解析测试]" | tee -a $LOG_FILE
nslookup $SERVER_IP 2>&1 | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE

# 5. 路由跟踪
echo "5. 网络路由跟踪" | tee -a $LOG_FILE
echo "-------------------" | tee -a $LOG_FILE
echo "[Traceroute到服务器]" | tee -a $LOG_FILE
if command -v traceroute >/dev/null 2>&1; then
    traceroute -m 15 $SERVER_IP 2>&1 | tee -a $LOG_FILE
elif command -v tracert >/dev/null 2>&1; then
    tracert -h 15 $SERVER_IP 2>&1 | tee -a $LOG_FILE
else
    echo "traceroute命令不可用" | tee -a $LOG_FILE
fi
echo "" | tee -a $LOG_FILE

# 6. 服务器地理位置和ISP信息
echo "6. 服务器信息查询" | tee -a $LOG_FILE
echo "-------------------" | tee -a $LOG_FILE
echo "[IP地址信息查询]" | tee -a $LOG_FILE
if command -v curl >/dev/null 2>&1; then
    echo "查询IP地址详细信息..." | tee -a $LOG_FILE
    curl -s "http://ip-api.com/json/$SERVER_IP" 2>/dev/null | python3 -m json.tool 2>/dev/null | tee -a $LOG_FILE || echo "IP信息查询失败" | tee -a $LOG_FILE
fi
echo "" | tee -a $LOG_FILE

# 7. 本地网络配置检查
echo "7. 本地网络配置" | tee -a $LOG_FILE
echo "-------------------" | tee -a $LOG_FILE
echo "[本地IP配置]" | tee -a $LOG_FILE
ifconfig | grep -A 1 "inet " | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
echo "[默认网关]" | tee -a $LOG_FILE
netstat -rn | grep default | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
echo "[DNS配置]" | tee -a $LOG_FILE
cat /etc/resolv.conf 2>/dev/null | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE

# 8. 云服务商特定检查
echo "8. 云服务商诊断建议" | tee -a $LOG_FILE
echo "-------------------" | tee -a $LOG_FILE
echo "根据服务器IP $SERVER_IP，建议检查以下项目：" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
echo "如果是阿里云服务器：" | tee -a $LOG_FILE
echo "- 登录阿里云控制台检查ECS实例状态" | tee -a $LOG_FILE
echo "- 检查安全组规则是否允许相应端口" | tee -a $LOG_FILE
echo "- 查看实例监控数据（CPU、内存、网络）" | tee -a $LOG_FILE
echo "- 检查是否欠费导致服务停止" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
echo "如果是腾讯云服务器：" | tee -a $LOG_FILE
echo "- 登录腾讯云控制台检查CVM实例状态" | tee -a $LOG_FILE
echo "- 检查安全组和网络ACL配置" | tee -a $LOG_FILE
echo "- 查看云监控数据" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
echo "如果是AWS服务器：" | tee -a $LOG_FILE
echo "- 检查EC2实例状态和健康检查" | tee -a $LOG_FILE
echo "- 验证Security Groups配置" | tee -a $LOG_FILE
echo "- 检查VPC和子网配置" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE

# 9. SSH连接详细测试
echo "9. SSH连接详细测试" | tee -a $LOG_FILE
echo "-------------------" | tee -a $LOG_FILE
echo "[SSH连接测试 - 详细模式]" | tee -a $LOG_FILE
echo "尝试SSH连接（10秒超时）..." | tee -a $LOG_FILE
timeout 10 ssh -v -o ConnectTimeout=10 -o StrictHostKeyChecking=no root@$SERVER_IP "echo 'SSH连接成功'" 2>&1 | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE

# 10. 生成诊断总结
echo "10. 诊断总结和建议" | tee -a $LOG_FILE
echo "-------------------" | tee -a $LOG_FILE
echo "基于以上测试结果，可能的问题原因：" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
echo "1. 服务器完全宕机" | tee -a $LOG_FILE
echo "   - 联系云服务商检查硬件状态" | tee -a $LOG_FILE
echo "   - 查看服务器控制台是否有异常" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
echo "2. 网络配置问题" | tee -a $LOG_FILE
echo "   - 检查安全组/防火墙规则" | tee -a $LOG_FILE
echo "   - 验证VPC和子网配置" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
echo "3. 服务停止" | tee -a $LOG_FILE
echo "   - SSH到服务器检查服务状态" | tee -a $LOG_FILE
echo "   - 重启相关服务" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
echo "4. 账户问题" | tee -a $LOG_FILE
echo "   - 检查是否欠费" | tee -a $LOG_FILE
echo "   - 验证账户权限" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE

echo "=== 诊断完成 ===" | tee -a $LOG_FILE
echo "详细日志已保存到: $LOG_FILE" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
echo "下一步建议：" | tee -a $LOG_FILE
echo "1. 查看完整的诊断日志" | tee -a $LOG_FILE
echo "2. 登录云服务商控制台检查服务器状态" | tee -a $LOG_FILE
echo "3. 如果服务器正常，检查应用程序日志" | tee -a $LOG_FILE
echo "4. 考虑联系云服务商技术支持" | tee -a $LOG_FILE